package util.translate;

import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;


public class GoogleTranslate {

    private static HttpPost httppost;
    private static HttpResponse response;
    private static final DefaultHttpClient httpclient = new DefaultHttpClient();

    /*
     * @param text 翻译原文
     * @param lanFrom 源语言
     * @param lanTo 目标语言
     * @return 翻译后的字符串
     */
    public static String translate(String text, String lanFrom, String lanTo) {
        List<NameValuePair> qparams = new ArrayList<NameValuePair>();
        qparams.add(new BasicNameValuePair("hl", "en"));
        qparams.add(new BasicNameValuePair("ie", "UTF-8"));
        qparams.add(new BasicNameValuePair("sl", lanFrom));
        qparams.add(new BasicNameValuePair("text", text));
        qparams.add(new BasicNameValuePair("tl", lanTo));

        //HttpClient提交post请求
        httppost = new HttpPost("http://translate.google.cn/translate_t#");

        String responseBody = "";
        String content = "";
        try {
            //将参数封装到post数据包中，
            httppost.setEntity(new UrlEncodedFormEntity(qparams, HTTP.UTF_8));
            response = httpclient.execute(httppost);
            responseBody = EntityUtils.toString(response.getEntity());

            //过滤出所需翻译后的内容
            int tmp1 = responseBody.indexOf("result_box");
            int tmp2 = responseBody.indexOf(">", tmp1);
            int tmp3 = responseBody.indexOf("</div>", tmp2);
            content = responseBody.substring(tmp2 + 1, tmp3).replaceAll("<br>", "\n").replaceAll("<[^>]*>", "");//替换换行符和其他网页标签
        } catch (Exception e) {
            return content;
        } finally {
            httppost.abort();
        }
        return content;
    }

    /*
     * 重载上面的translate()方法，实现文本数组的翻译
     */
    public static String[] translate(String[] text, String lanFrom, String lanTo) {
        if (text == null || text.length < 1) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < text.length; i++) {
            if (i < text.length - 1) {
                sb.append(text[i]).append("BBaaBB");
            } else {
                sb.append(text[i]);
            }
        }
        return translate(sb.toString(), lanFrom, lanTo).split("BBaaBB");
    }

    public String getTString(String sString) {
        String tString = GoogleTranslate.translate(sString, "zh-CN", "zh-TW");
        tString = tString.replaceAll("&#160;", "'").replaceAll("&lt;", "<").replaceAll("&gt;", ">").replaceAll("&amp;", "&").replaceAll("&apos;", "'").replaceAll("&quot;", "\"").replaceAll("&#8203;", "")
        		.replaceAll("&quot;", "").replaceAll("\\s*<\\s*", "<").replaceAll("\\s*>\\s*", ">").replaceAll("&#39;", "'").replaceAll("\\n", "");
		return tString;
       
    }
}
